package com.wh.shiroswagger.controller;

import com.wh.shiroswagger.exception.BusinessException;
import com.wh.shiroswagger.pojo.User;
import com.wh.shiroswagger.result.DataResult;
import com.wh.shiroswagger.result.ResponseCodeMsg;
import com.wh.shiroswagger.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Auther: HuiEr
 * @Date: 2020/3/5 20:54
 * @Description
 */
@RestController
@Api(tags = "用户控制器接口")
public class UserController {

    @Autowired
    private UserService userService;
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ApiOperation(value = "这是登录接口")
    public DataResult<User> login(User user){
        DataResult<User> result=null;
        try{
            User login = userService.login(user);
            result=DataResult.success(ResponseCodeMsg.SUCCESS,login);
        }catch (Exception e){
            if(e instanceof BusinessException){
                BusinessException err=(BusinessException)e;
                result=DataResult.exceptionOccur(err.getExcode(),err.getExmsg());
                return result;
            }else {
                result=DataResult.exceptionOccur(ResponseCodeMsg.SYSTEM_EXCEPTION);
                return result;
            }
        }
        return result;
    }


    @RequestMapping(value = "/user/list",method = RequestMethod.GET)
    @ApiImplicitParam(name="token",value = "token的值",required = true,paramType = "header",dataType = "String")
    @ApiOperation(value = "这里是获取用户列表")
    public DataResult<List<User>> findUserList(){
        DataResult<List<User>> userlistResult=null;
        try{
            List<User> userList = userService.findUserList();
            userlistResult=DataResult.success(ResponseCodeMsg.SUCCESS,userList);
        }catch (Exception e){
            userlistResult=DataResult.exceptionOccur(ResponseCodeMsg.SYSTEM_EXCEPTION.getCode(),ResponseCodeMsg.SYSTEM_EXCEPTION.getMsg()+e.getMessage());
            return userlistResult;
        }
        return userlistResult;
    }
}
